export enum EaseType {
    None, SineIn, SineOut, SineInOut
}

export default class MathUtils {

    /**
     * 获取贝塞尔曲线上所有的点
     */
    static getBezierPoints(p1: cc.Vec2, p2: cc.Vec2, p3: cc.Vec2, pointsAmount: number, easeType: EaseType = EaseType.None): cc.Vec2[] {
        let amount = pointsAmount - 1;
        let newVector2: cc.Vec2[] = [];
        let t = 0;//全程0-1
        for (let ik = 0; ik <= amount; ik++) {
            let x = MathUtils.calculateQuadSpline(p1.x, p2.x, p3.x, t);
            let y = MathUtils.calculateQuadSpline(p1.y, p2.y, p3.y, t);
            newVector2[ik] = cc.v2(x, y);
            if (easeType == EaseType.None) {
                t = t + 1 / amount;
            } else if (easeType == EaseType.SineIn) {
                t = MathUtils.sineEaseIn(ik, 0, 1, amount - 1);
            } else if (easeType == EaseType.SineOut) {
                t = MathUtils.sineEaseOut(ik, 0, 1, amount - 1);
            } else if (easeType == EaseType.SineInOut) {
                t = MathUtils.sineEaseInOut(ik, 0, 1, amount - 1);
            }
        }
        newVector2[amount] = p3;
        return newVector2;
    }
    private static calculateQuadSpline(z0: number, z1: number, z2: number, t: number): number {
        let a1 = (1.0 - t) * (1.0 - t) * z0;
        let a2 = 2.0 * t * (1 - t) * z1;
        let a3 = t * t * z2;
        let a4 = a1 + a2 + a3;
        return a4;
    }

    private static sineEaseIn(t: number, b: number, c: number, d: number) {
        return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
    }

    private static sineEaseOut(t: number, b: number, c: number, d: number) {
        return c * Math.sin(t / d * (Math.PI / 2)) + b;
    }

    private static sineEaseInOut(t: number, b: number, c: number, d: number) {
        return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;
    }

}
